package cn.wlinker.video.monitor.service;

import cn.hutool.core.util.StrUtil;
import cn.wlinker.video.monitor.brand.hikversion.HikVisionAccessPlatformEnum;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author gxsjx
 * @version 1.0
 * @date 2022/7/26
 */
public interface IVideoAccessPlatformEnum {

    ConcurrentHashMap<String, IVideoAccessPlatformEnum> suppliers = new ConcurrentHashMap<>(8);

    /**
     * 获取Key
     *
     * @return
     */
    default String getKey() {
        return getKey(getVideoBrandEnum(), getAccessPlatform());
    }

    /**
     * 获取Key
     *
     * @param videoBrandEnum
     * @param accessPlatform
     * @return
     */
    static String getKey(IVideoBrandEnum videoBrandEnum, String accessPlatform) {
        return StrUtil.format("{}-{}", videoBrandEnum.getMonitorType(), accessPlatform);
    }

    /**
     * 获取实例
     * @param videoBrandEnum
     * @param accessPlatform
     * @return
     */
    static IVideoAccessPlatformEnum getOne(IVideoBrandEnum videoBrandEnum, String accessPlatform) {
        HikVisionAccessPlatformEnum.init();
        String key = getKey(videoBrandEnum, accessPlatform);
        if (suppliers.containsKey(key)) {
            return suppliers.get(key);
        }
        throw new RuntimeException("没有找到对应的实例");
    }

    static void register(IVideoAccessPlatformEnum videoAccessPlatformEnum) {
        String key = videoAccessPlatformEnum.getKey();
        suppliers.put(key, videoAccessPlatformEnum);
    }

    static void register(IVideoAccessPlatformEnum[] videoAccessPlatformEnums) {
        for (IVideoAccessPlatformEnum videoAccessPlatformEnum : videoAccessPlatformEnums) {
            register(videoAccessPlatformEnum);
        }
    }

    static void register(Collection<IVideoAccessPlatformEnum> protocolTypeEnums) {
        protocolTypeEnums.forEach(IVideoAccessPlatformEnum::register);
    }

    /**
     * 获取当前的视频品牌
     *
     * @return
     */
    IVideoBrandEnum getVideoBrandEnum();

    /**
     * 获取构建流的工厂
     *
     * @return
     */
    String getAccessPlatform();

    /**
     * 获取构建流的工厂
     *
     * @return
     */
    IVideoStreamService getFactory();
}
